<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" >
  <!-- <el-form-item label="司机id" > 
    <el-input v-model="ruleForm.driverId" placeholder="请输入已有司机id"></el-input>
  </el-form-item> -->
  <el-form-item label="司机工号">
   <el-select v-model="ruleForm.userId" placeholder="司机工号">
    <el-option
      v-for="item in driverList"
      :key="item.userId"
      :label="item.userId"
      :value="item.userId">
    </el-option>
  </el-select>
  </el-form-item >
  <el-form-item label="发起人姓名">
   <el-select v-model="ruleForm.applyUserId" placeholder="发起人姓名">
    <el-option
      v-for="item in allUserList"
      :key="item.userId"
      :label="item.name"
      :value="item.userId">
    </el-option>
  </el-select>
  +
   <el-select v-model="ruleForm.driverId" placeholder="发起人工号">
    <el-option
      v-for="item in allUserList"
      :key="item.userId"
      :label="item.userId"
      :value="item.userId">
    </el-option>
  </el-select>
  </el-form-item>
  <el-form-item label="车牌号">
   <el-select v-model="ruleForm.carId" placeholder="车牌号">
    <el-option
      v-for="list in cartsList"
      :key="list.carId"
      :label="list.chepai"
      :value="list.chepai">
    </el-option>
  </el-select>
  </el-form-item >
  <el-form-item label="出发地点" >
    <el-input v-model="ruleForm.setoffAddress"></el-input>
  </el-form-item>
  <el-form-item label="到达地点" >
    <el-input v-model="ruleForm.arrivalAddress"></el-input>
  </el-form-item>
  <el-form-item label="乘车人数" >
    <el-input v-model="ruleForm.passengersNum"></el-input>
  </el-form-item>
  <el-form-item label="里程数" >
    <el-input v-model="ruleForm.totalKM"></el-input>
  </el-form-item>
  <el-form-item label="出发时间" required>
    <el-col :span="11">
      <el-form-item prop="setoffTime">
        <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.setoffTime" style="width: 100%;"></el-date-picker>
      </el-form-item>
    </el-col>
    <el-col class="line" :span="2">-</el-col>
    <el-col :span="11">
      <el-form-item prop="setoffTime">
        <el-time-picker placeholder="选择时间" v-model="ruleForm.setoffTime" style="width: 100%;"></el-time-picker>
      </el-form-item>
    </el-col>
  </el-form-item> 
<el-form-item label="新增任务" >
  <el-tabs v-model="activeName" type="card">
    <el-tab-pane 
    v-for="(pred,key) in taskList "
    :key="key"
    :label="key" :name="key">
  <el-table
    :data="taskList[key]"
    border
    style="width: 40%">
    <el-table-column
      prop="taskName"
      label="任务类型"
      width="180">
    </el-table-column>
    <el-table-column
      label="次数"
      width="180">
    <template slot-scope="scope">
      <input  placeholder="请输入次数" :name="key" v-model="scope.row.inputVal"  style="width:100px;height:30px;border:none;outline:none" />
    </template>
    </el-table-column>
      <el-table-column
      label="操作"
      width="180">
      <template slot-scope="scope">
      <el-button @click="tijiao(scope.row.id,scope.row.inputVal)">确定</el-button>
      </template>

    </el-table-column>
  </el-table>
    </el-tab-pane>

  </el-tabs>
</el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submint()">提交</el-button>
    <el-button >重置</el-button>
  </el-form-item>
</el-form>
</template>
<script>
import Vue from 'vue'
import request from '../api/request.js'
import {
  getRequestHeader,
  loginApi,
  getThirdCompanyApi,
  updateToken,
  getCookie,
  checkLoginStatusApi 
} from "../api/user.js"
  export default {
    inject: ['reload'],
    data() {
      return {
      activeName: '计次任务',
      taskList:{},
      input:'',
      cartsList:[],
       driverList:[],
       allUserList:[],
       TaskAndOrderMapping:{},
          mappings:[],
        ruleForm: {
          driverId: '',
          applyUserId: '',
          setoffAddress: '',
          setoffTime: '',
          arrivalAddress: '',
          passengersNum: '',
          totalKM:'',
          userId:'',
          carId:''
        },
        rules: {
          driverId: [
            { required: true, message: '请输入司机id', trigger: 'blur' },
          ],
          applyUserId: [
            { required: true, message: '请输入发起人工号', trigger: 'change' }
          ],
          setoffTime: [
            { type: 'date', required: true, message: '请选择日期', trigger: 'change' }
          ],
          setoffTime: [
            { type: 'date', required: true, message: '请选择时间', trigger: 'change' }
          ],
          arrivalAddress: [
            { required: true, message: '请输入达到地点', trigger: 'change' }
          ],
          passengersNum: [
            { required: true, message: '请输入乘车人数', trigger: 'blur' }
          ],
          totalKM: [
            { required: true, message: '请输入里程数', trigger: 'blur' }
          ]
        }
      };
    },
    created(){
      this.getInfoList()
      this.getAllTask()
      this.getAllDriver()
      this.getAllCarts()
    },
    methods: {
      //  确定
      tijiao(id,value){
         var TaskAndOrderMapping = {
            taskId:id,
            total:value
         }
         this.mappings=[TaskAndOrderMapping]
      },
      //获取所有任务
      getAllTask(){
        request({
          url:'/order/getAllTask',
          method:'post',
        }).then(res => {
          console.log(res.data)
          this.taskList = res.data
          console.log(this.taskList)
          for (let key in this.taskList) {
            for (let item of this.taskList[key]) {
               Vue.set(item,'inputVal','')
            }
          }
        })
      },
      //获取所有司机接口
      getAllDriver(){
      const token = getCookie("tokenx")
      console.log(token)
        request({
          url:'/user/getDrivers',
          method:'POST',
          data:{
            "token":token,
            "page": "1",
            "pagesize": "10",
            "filter": ''
          }
        }).then(res => {
          this.driverList = res.data.data.list
        })
      },
      //获取所有车数据
      getAllCarts(){
      const token = getCookie("tokenx")
        request({
          url:'/car/getCars',
          method:'POST',
          data:{
            "token":token,
            "page": "1",
            "pagesize": "10",
            "filter": ''
          }
        }).then(res => {
          console.log(res.data.data.list)
          this.cartsList = res.data.data.list
          // this.driverList = res.data.data.list
        })
      },
      getInfoList(){
        request({
          url:'/user/getAllUserxes',
          method:'post',
        }).then(res => {
          this.allUserList = res.data
        })
      },
      submint(){
        var that= this
        let ruleForm= that.ruleForm
        let mappings = that.mappings
        console.log(ruleForm)
        // let mappings= this.mappings
        let order = {ruleForm,mappings}
        request({
        url: '/order/creatOrder',
        method: 'post',
        data: {
          "order":order
        }
    }).then(res=>{
            this.$confirm('创建订单成功')
            this.reload()
        }).catch(res => {
          console.log(err)
        })
      }
    }
  }
</script>